<template>
	<view>

		<u-toast ref="uToast" />

		<u-modal :show="showModel" title="请先绑定您的erp账号" confirmText='绑定' @confirm='userModel'>
			<view class="slot-content">
				<view>
					<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
					<u--form labelPosition="left" :model="userInfo" :rules="rules" ref="form1">
						<u-form-item label="姓名" prop="userName" borderBottom required>
							<u--input v-model="userInfo.userName" border="none"></u--input>
						</u-form-item>
						<u-form-item label="账号" prop="userCode" borderBottom required>
							<u--input v-model="userInfo.userCode" border="none"></u--input>
						</u-form-item>
						<!-- <u-form-item label="密码" prop="password" borderBottom required>
							<u-input :password-icon="true" :border="none" type="password" v-model="userInfo.password" ></u-input>
						</u-form-item> -->
						<u-form-item label="密码" prop="password" borderBottom required>
							<u--input  :border="none" :type="isOpen?'text':'password'"  v-model="userInfo.password">
									<!-- <template slot="suffix">
										
										<u-icon  @tap="isOpen=!isOpen" :name="isOpen?'eye': 'eye-off'"></u-icon>
									</template> -->
							</u--input>
							<u-icon slot="right"  @click="isOpen=!isOpen" :name="isOpen?'eye':'eye-off'"></u-icon>
						</u-form-item>
						
						

					</u--form>

				</view>
			</view>
		</u-modal>
		
		
		
		<u-picker :show="showSelector" :columns="columns" keyName="companyName"  visibleItemCount='5' 
		 @confirm='selectorQ' title='当前门店'></u-picker>
		
		
	</view>
</template>

<script>
	import store from '@/store/index.js';
	export default {
		data() {
			return {
				showModel: false,
				showSelector:false,
				isOpen:false,
				messageNotify:'',
				columns:[],
				userInfo: {
					userName: '', //姓名
					userCode: '', //账号
					password:'',//密码
				},

				rules: {
					'userName': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'userCode': {
						type: 'string',
						required: true,
						message: '请填写账号',
						trigger: ['blur', 'change']
					},
					'password': {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur', 'change']
					}

				}
			}
		},
		methods: {
			selectorQ(e){
			    var that = this
				that.$store.commit("changeUser",e.value[0])
				uni.setStorageSync('changeUser', e.value[0]);
				setTimeout(function() {
					uni.switchTab({
						url: '/pages/sys/msg/index'
					})
				}, 500)
			},

			userModel() {
				var that = this
				this.$refs.form1.validate().then(res => {
					uni.showLoading({
						title: '加载中',
						mask: true,
						icon: 'loading',
					});
					that.$request('bindErpAccount', that.userInfo, 'get').then(res2 => {
						console.log(res2)
						if (res2.code == '200') {
							uni.$u.toast(`绑定成功${res2.message}`)
							var obj = {}
							obj.userCode = res2.data.userCode
							obj.userName = res2.data.userName
							that.$store.commit("changeUserList",obj)
							uni.setStorageSync('changeUserList', obj);
							if(res2.data.companyList.length == 1){
								that.$store.commit("changeUser", res2.data.companyList[0])
								uni.setStorageSync('changeUser', res2.data.companyList[0]);
								setTimeout(function() {
									uni.switchTab({
										url: '/pages/sys/msg/index'
									})
								}, 1000)
							}else if(res2.data.companyList.length == 0){
								uni.$u.toast('没有权限门店,请联系erp管理员处理')
							}else{
								that.showSelector = true
								that.columns.push(res2.data.companyList)
							}
							
							
							uni.hideLoading();
						} else {
							that.$refs.uToast.error(`登录失败${res2.data.msg}`)
							uni.hideLoading();
						}
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		},
		onLoad() {
			var that = this
			var userInfo;
			
			uni.showLoading({
				title: '加载中',
				mask: true,
				icon: 'loading',
			});
			wx.login({
				success(res) {
					if (res.code) {
						//发起网络请求
						that.$request('getUnionIdByJsCode', {
							jsCode: res.code
						}, 'get').then(res1 => {
							if (res1.data.errCode) {
								that.$refs.uToast.error(`登录失败${res1.data.errMsg}`)
							} else {
								userInfo = res1.data
								that.userInfo = {
									...that.userInfo,
									...userInfo
								}
								that.$request('checkHaveErpAccount', {
									unionId: userInfo.unionid
								}, 'get').then(res2 => {
									if (res2.data.accountNotFound == '30001') {
										that.$refs.uToast.error(`暂未绑定${res2.data.accountNotFound}`)
										setTimeout(function() {
											that.showModel = true
										}, 1000)
										uni.hideLoading();
									} else {
                                        if(store.state.userList.companyCode) {
											uni.switchTab({
												url: '/pages/sys/msg/index'
											})
										}else{
											that.$request('selectCompany', {
												unionid: userInfo.unionid
											}, 'get').then(res2 => {
												if (res2.code == '200') {
													var obj = {}
													obj.userCode = res2.data.userCode
													obj.userName = res2.data.userName
													that.$store.commit("changeUserList",obj)
													uni.setStorageSync('changeUserList', obj);
													uni.setStorageSync('companyList', res2.data.companyList);
													that.$store.commit("setcompanyList",res2.data.companyList)
													
													if(res2.data.companyList.length == 1){
														that.$store.commit("changeUser", res2.data.companyList[0])
														uni.setStorageSync('changeUser', res2.data.companyList[0]);
														setTimeout(function() {
															uni.switchTab({
																url: '/pages/sys/msg/index'
															})
														}, 1000)
													}else if(res2.data.companyList.length == 0){
														uni.$u.toast('没有权限门店,请联系erp管理员处理')
													}else{
														that.showSelector = true
														that.columns.push(res2.data.companyList)
													}
													uni.hideLoading();
												} else {
													uni.$u.toast(`绑定失败${res2.data}`)
													uni.hideLoading();
												}
											})
										}

										
									}
								})
							}
						})
					} else {
						that.$refs.uToast.error(`登录失败${res.errMsg}`)
						uni.hideLoading();
					}
				}
			})
		}
	}
</script>

<style lang="scss">
	.grid-text {
		font-size: 14px;
		color: #909399;
		padding: 10rpx 0 20rpx 0rpx;
		/* #ifndef APP-PLUS */
		box-sizing: border-box;
		/* #endif */
	}
	/deep/.u-picker{
		height: 65vh !important;
	}
</style>
